/*
 * @Author: Tendo 
 * @Date: 2021-09-14 18:58:52 
 * @param {ListNode} head
 * @return {ListNode}
 */
var detectCycle = function(head) {
    if(!head || !head.next) return null;
    let slow = head.next, 
        fast = head.next.next;
    while (fast && fast.next && fast!=slow){
        slow = slow.next;
        fast = fast.next.next;
    }
    if (slow == fast) {//有环
        let index1 = fast, //让一个指针指向相遇的节点
            index2 = head; //一个指针指向头节点
        // 两个指针，从头节点和相遇节点，各走一步，直到相遇，相遇点即为环入口
        while (index1 != index2) {
            index1 = index1.next;
            index2 = index2.next;
        }
        return index1
    }
    return null;
};